"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC参数--资源消耗
Case Name   : 配置enable_global_plancache=on，
              local_syscache_threshold最小只能设置为16MB
Create At   : 2023/04/18
Owner       : @kyeleze
Description :
        1、配置enable_global_plancache=on; local_syscache_threshold=15MB，
           重启数据库
        2、配置enable_global_plancache=on、local_syscache_threshold=16MB,
           重启数据库，查看配置
Expect      :
        1、设置成功，数据库重启失败
        2、重启成功，enable_global_plancache=on, local_syscache_threshold=16MB
History     :
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class GUC(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.dirname(__file__)} start----')
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.com = Common()
        self.constant = Constant()
        self.global_plancache = self.com.show_param("enable_global_plancache")
        self.syscache_threshold = \
            self.com.show_param("local_syscache_threshold")

    def test_startdb(self):
        step = "--step1:配置enable_global_plancache=on; " \
               "local_syscache_threshold=15MB,重启数据库 " \
               "expect: 设置成功，数据库重启失败--"
        self.log.info(step)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "enable_global_plancache=on")
        self.assertTrue(result)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "local_syscache_threshold=15MB")
        self.assertTrue(result)
        result = self.primary_sh.restart_db_cluster()
        self.assertFalse(result)

        step = "--step2:配置enable_global_plancache=on、" \
               "local_syscache_threshold=16MB，重启数据库，查看配置" \
               "expect:重启成功，enable_global_plancache=on, " \
               "local_syscache_threshold=16MB--"
        self.log.info(step)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "local_syscache_threshold=16MB")
        self.assertTrue(result)
        result = self.primary_sh.restart_db_cluster()
        self.assertTrue(result)
        result = self.primary_sh.execut_db_sql("show enable_global_plancache;")
        self.assertIn("on", result, "执行失败" + step)
        result = self.primary_sh.execut_db_sql("show local_syscache_threshold;")
        self.assertIn("16MB", result, "执行失败" + step)

    def tearDown(self):
        step = "--恢复环境--"
        self.log.info(step)
        set_result1 = self.primary_sh.execute_gsguc(
            "set",
            self.constant.GSGUC_SUCCESS_MSG,
            f"enable_global_plancache={self.global_plancache}")
        self.log.info(set_result1)
        set_result2 = self.primary_sh.execute_gsguc(
            "set",
            self.constant.GSGUC_SUCCESS_MSG,
            f"local_syscache_threshold={self.syscache_threshold}")
        self.log.info(set_result2)
        start_result = self.primary_sh.restart_db_cluster()
        self.assertTrue(set_result1)
        self.assertTrue(set_result2)
        self.assertTrue(start_result)
        self.log.info(f'----{os.path.dirname(__file__)} end----')

